package codeRose.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import codeRose.domain.Customer;
import codeRose.domain.Goods;
import codeRose.domain.Worker;
import codeRose.service.UserService;

/**
 * @author Administrator
 *
 */
@Controller
public class UserController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    /**
     * 鐧诲綍/login
     */
    @RequestMapping(value="/login")
    public ModelAndView login(String name,String password,String identify,ModelAndView mv,HttpSession session){
        Customer customer=userService.login(name, password, identify);
        System.out.println(customer);
        if(customer!=null){
            session.setAttribute("user", customer);
            System.out.println(customer.getId());
            mv.setViewName("index");
        }else {
            Worker worker = userService.Mlogin(name, password, identify);
            System.out.println(worker);
            if(worker!=null){
            	session.setAttribute("user", worker);
            	System.out.println("00aaa");
            	System.out.println(worker.getPower());
            	if(worker.getPower().equals("add")){
            		mv.setViewName("managerA");
            		System.out.println("aaaa");
            	}else if(worker.getPower().equals("confirm")){
            		mv.setViewName("managerB");
            		System.out.println("bbbb");
            	}
            }else{
            mv.addObject("message","登陆失败");
            mv.setViewName("login");}
            }
      
        return mv;
    }
    /**
     * 娉ㄥ唽
     *
     */
   @RequestMapping(value="/signup")
   public ModelAndView signup(String name,String password,String nickname,String email,String sex,String address,String phone,ModelAndView mv,HttpSession session ){
	int a = userService.signup(name, password, nickname,email,sex,address, phone);
	if(a!=0){
	mv.addObject("message","注册成功请登录");
	mv.setViewName("login");
	}else{
	mv.addObject("message","注册失败请重新注册");
	mv.setViewName("reg");
	}
	return mv;
   }
   /**
    * 显示全部的商品
    *
    **/
   @RequestMapping(value="/show")///{id}//@PathVariable(value="id")
   public ModelAndView listGoods(ModelAndView mv,HttpSession session){
	List<Goods> list = userService.listGoods();
	System.out.println(list);
	if(list!=null){
			session.setAttribute("list", list);
			mv.setViewName("index");
	}else{
		mv.addObject("message","没有商品信息");
		mv.setViewName("index");
	}
	return mv;   
   }
   /*
   *//**
    * 添加商品(A管理员加货)
    *
    */
   @RequestMapping(value="/addgoods")
   public ModelAndView addGoods(String name,Integer price,String type,String discount,String createtime,String limittime,String productby,String size,String pic,Integer number,ModelAndView mv,HttpSession session){
	int c= userService.addGoods(name, price, type, discount, createtime, limittime, productby, size, pic,number);
	   if(c!=0){
		   mv.addObject("message","加货成功，等待B管理员上架");
		 mv.setViewName("managerA");  
	   }else{
		   mv.addObject("message","加货失败，仓库可能已经溢出了");
		   mv.setViewName("addgoods");
	   }
	   return mv;
     }
   @RequestMapping(value="/confirm")
   public ModelAndView confirm(Integer id,ModelAndView mv,HttpSession session){
	   int d=userService.confirm(id);
	   if(d!=0){
		   mv.addObject("message","确认成功,商品已经成功上架");
		   mv.setViewName("managerB");
		   	   }else{
		   		   mv.addObject("message","确认失败,请分析原因");
		   		   mv.setViewName("confirm");
		   	   }
	  return mv; 
   }
  
   /**
    * 鍒犻櫎璁㈠崟
    *
    *//*
   @RequestMapping(value="/delete")
   public ModelAndView deleteGoods(ModelAndView mv,HttpSession session){
	   System.out.println("gg");
	   
	int gg = (int) session.getAttribute("gid");   
	int b=userService.deleteGoods(gg);
	if(b!=0){
		mv.addObject("message","鍒犻櫎鎴愬姛锛岃鍒锋柊");
		mv.setViewName("main");
	}else{
		 mv.addObject("message","鍒犻櫎澶辫触锛岃閲嶆柊灏濊瘯");
		   mv.setViewName("delete");
	}
	   
	   return mv;   
   }*/
  
}